﻿using System;
using System.IO;

namespace Lib.MonitLogs.Entitys;
/// <summary>
/// 
/// </summary>
internal class LogEntry {
    /// <summary>
    /// 
    /// </summary>
    public static LogEntry Create(string Key, string Content) {
        var edKey = $"{DateTime.Now:yyyyMMdd}.{Key}";
        LogEntry Re;
        if (Logs.LogsKeys.TryGetValue(edKey, out LogEntry Value)) {
            Re = Value;
        } else {
            Re = Initialize(Key);
        }
        Re = Re.Clone();
        Re.Content = Content;
        return Re;
    }
    /// <summary>
    /// 
    /// </summary>
    private static LogEntry Initialize(string Key) {
        var Re = new LogEntry();
        var Folder =$"{Logs.LogPath}{DateTime.Now:yyyyMM}";
        if (!Directory.Exists(Folder)) {
            Directory.CreateDirectory(Folder);
        }
        Re.Path = $"{Folder}/{DateTime.Now:dd}.{Key}.txt";
        Re.Day = DateTime.Now.Day;
        Key = $"{DateTime.Now:yyyyMMdd}.{Key}";
        Logs.LogsKeys.TryAdd(Key, Re);
        return Re;
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    private LogEntry() { }
    /// <summary>
    /// 
    /// </summary>
    public string Path { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int Day { get; set; }
    /// <summary>
    /// 日志内容
    /// </summary>
    public string Content { get; set; }
    /// <summary>
    /// 获取当前对象的浅表副本
    /// </summary>
    public LogEntry Clone() {
        return (LogEntry)MemberwiseClone();
    }

}

